import requests
import json
import re

from lxml import etree

page = 1
while True:
    url = "https://www.zongheng.com/api2/catefine/storeSearch"
    data_json = {
        'worksTypes': 0,
        'bookType': 1,
        'subWorksTypes': 0,
        'totalWord': 0,
        'serialStatus': 1,
        'vip': 9,
        'order': 'weekOrder',
        'pageNum': page,
        'pageSize': 20,
        'categoryId': 0,
        'categoryPid': 0,
        'naodongFilter': 0,
    }

    response = requests.post(url,json=data_json)
    html = json.loads(response.text)
    if not html:
        break
    for i in range(0,len(html["result"]["bookList"])):
        name = html["result"]["bookList"][i]['name']
        bookId = html["result"]["bookList"][i]['bookId']
        book_url = f"https://huayu.zongheng.com/book/{bookId}.html"
        res = requests.get(book_url)
        book_html = res.text
        root = etree.HTML(book_html)
        nums_divs = root.xpath('//div[@class="nums"]')
        book_dec_divs = root.xpath('//div[@class="book-dec Jbook-dec hide"]')
        all_catalog_btns = root.xpath('//a[@class="all-catalog"]')
        for nums_div, book_dec_div, all_catalog_btn in zip(nums_divs, book_dec_divs, all_catalog_btns):
            word_count = nums_div.xpath('.//span[contains(text(), "字数")]/i/text()')[0].strip()
            total_recommend = nums_div.xpath('.//span[contains(text(), "总推荐")]/i/text()')[0].strip()
            total_click = nums_div.xpath('.//span[contains(text(), "总点击")]/i/text()')[0].strip()
            weekly_recommend = nums_div.xpath('.//span[contains(text(), "周推荐")]/i/text()')[0].strip()
            text_content = ''.join(book_dec_div.xpath('.//p//text()')).strip()
            href = all_catalog_btn.attrib['href']
            print("=" * 100)
            print("小说名称",name)
            print("字数:", word_count)
            print("总推荐:", total_recommend)
            print("总点击:", total_click)
            print("周推荐:", weekly_recommend)
            print("简介:", text_content)
            chapter_res = requests.get(href)
            chapter_html = etree.HTML(chapter_res.text)
            links = chapter_html.xpath('//li[@class=" col-4"]/a')
            for link in links:
                text = link.text
                href = link.get('href')
                print(text)
                print("-"*100)
                story_res = requests.get(href)
                story_html = etree.HTML(story_res.text)
                content_div = story_html.xpath('//div[@class="content"]')[0]
                paragraphs = content_div.xpath('.//p')
                story_content = '\n'.join([p.text.strip() for p in paragraphs if p.text])
                print(story_content)
                print("-" * 100)
    page += 1